﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;
using DataLib.Common;
using Ewts.Code;

namespace Ewts.RDLC
{
    public partial class GoodsInfoRDLC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ReportViewer1.LocalReport.EnableHyperlinks = true;
                //参数为LoadNumID
                ListItem item = new ListItem();
                item.Text = "--请选择--";
                item.Value = "";
                DataLib.BLL.City bllLib = new DataLib.BLL.City();

                // 处理地
                dropGcity.DataSource = bllLib.GetList(" flag1 ='02'");
                dropGcity.DataTextField = "cityname";
                dropGcity.DataValueField = "citycode";
                dropGcity.DataBind();
                dropGcity.Items.Insert(0, item);


                // 归属地
                dropBcity.DataSource = bllLib.GetList(" flag1 ='01'");
                dropBcity.DataTextField = "cityname";
                dropBcity.DataValueField = "citycode";
                dropBcity.DataBind();
                dropBcity.Items.Insert(0, item);
                bindMonth();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strbelong = dropBcity.SelectedValue;
            string strdis = dropGcity.SelectedValue;
            string gcode = txtGoodsCode.Text;
            string stroageno = txtStoragePos.Text;
            string startdate = txtTime1.Text + " " + txtStartH.Value + ":" + txtStartM.Value + ":" + txtStartS.Value;
            string enddate = txtTime2.Text + " " + txtEndH.Value + ":" + txtEndM.Value + ":" + txtEndS.Value;
            string cleanmonth = hidcleanmonth.Value;

            string strErr = "";
            if (txtTime1.Text.Trim() == "" || txtTime2.Text.Trim() == "" || txtStartH.Value.Trim() == "" || txtStartM.Value.Trim() == ""
                || txtEndS.Value.Trim() == "" || txtEndH.Value.Trim() == "" || txtEndM.Value == "" || txtStartS.Value.Trim() == "")
            {
                strErr += "请选择时间段!";
            }
            if (!PageValidate.IsDateTime(startdate) || !PageValidate.IsDateTime(enddate))
            {
                strErr += "开始和结束日期不得为空且必须为日期类型！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            DataTable goodsInfoDT = new DataLib.BLL.StorageData().getDsByDate(DateTime.Parse(startdate), DateTime.Parse(enddate), strbelong, strdis, gcode, stroageno);

            BindData(goodsInfoDT);
        }


        protected void BindData(DataTable goodsInfoDT)
        {
            //为列表绑定数据
            ReportDataSource rds = new ReportDataSource("DataSet1", goodsInfoDT);
            ReportViewer1.LocalReport.DataSources.Clear();
            new RDLCPrinter().FillRow(goodsInfoDT, 3, 30, 30);//为列表添加空行
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
        }

        /// <summary>
        /// 绑定财务结算月
        /// </summary>
        protected void bindMonth()
        {
            int year = DateTime.Now.Year;

            ListItem listitem = new ListItem((year + 1).ToString() + "年", (year + 1).ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem(year.ToString() + "年", year.ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem((year - 1).ToString() + "年", (year - 1).ToString());
            dropYear.Items.Add(listitem);

            dropYear.SelectedValue = year.ToString();
            dropMonth.SelectedValue = DateTime.Now.Month.ToString().PadLeft(2, '0');

        }

    }
}